package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@SuppressWarnings({"all"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;
    @Autowired
    private UserAccountService userAccountService;
    @Autowired
    private UserAccountDetailMapper userAccountDetailMapper;

    /**
     * 调用微信接口支付成功,修改充值订单状态
     *
     * @param orderNo
     * @return
     */
    @Override
    public void rechargePaySuccess(String orderNo) {
        //根据订单信息查询充值记录,更新充值状态
        RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(new LambdaQueryWrapper<RechargeInfo>()
                .eq(RechargeInfo::getOrderNo, orderNo));
        rechargeInfo.setRechargeStatus(SystemConstant.PAYMENT_STATUS_PAID);
        rechargeInfoMapper.updateById(rechargeInfo);
        //更新用户金额信息
        boolean updated = userAccountService.update(null, new LambdaUpdateWrapper<UserAccount>()
                .eq(UserAccount::getUserId, rechargeInfo.getUserId())
                .setSql("total_amount = total_amount+" + rechargeInfo.getRechargeAmount())
                .setSql("available_amount = available_amount + " + rechargeInfo.getRechargeAmount())
                .setSql("total_income_amount = total_income_amount + " + rechargeInfo.getRechargeAmount()));
        if (updated) {
            //更新充值数据成功,新增金额交易日志
            userAccountService.saveUserAccountDetail(rechargeInfo.getUserId(), "微信充值", rechargeInfo.getRechargeAmount(), rechargeInfo.getOrderNo(), SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT);
        }
    }

    @Override
    public Page<UserAccountDetail> findUserRechargePage(Page<UserAccountDetail> userAccountDetailPage, Long userId, String tradeType) {
        userAccountDetailMapper.selectPage(userAccountDetailPage, new LambdaQueryWrapper<UserAccountDetail>()
                .eq(UserAccountDetail::getUserId, userId)
                .eq(UserAccountDetail::getTradeType, tradeType)
                .orderByDesc(UserAccountDetail::getId));
        return userAccountDetailPage;
    }
}
